<?php

namespace app\models;

use app\tools\Re;
use app\tools\SMS;
use Yii;

/**
 * This is the model class for table "phone_code".
 *
 * @property int $id
 * @property string $phone 手机号
 * @property string $code 验证码
 * @property string $text 文本内容
 * @property int $status 短信状态
 * @property string $scene 使用场景
 * @property int $created_at
 */
class PhoneCode extends Base
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'phone_code';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ['phone', 'required'],
            [['status', 'created_at'], 'integer'],
            [['phone'], 'string', 'length' => 11],
            [['scene'], 'string', 'max' => 64],
            [['code'], 'string', 'max' => 6],
            [['text'], 'string', 'max' => 128],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'phone' => '手机号',
            'code' => '验证码',
            'text' => '文本内容',
            'status' => '短信状态',
            'scene' => '使用场景',
            'created_at' => 'Created At',
        ];
    }

    const UNCHECK = 0; // 未校验 0
    const CHECKED = 1; // 已校验 1
    const SEND_FAIL = -1; // 发送失败

    public function beforeSave($insert)
    {
        if($insert){
            $this->setPhoneText();
        }
        return parent::beforeSave($insert);
    }

    public function setPhoneText(){
        $this->text = '您的验证码为：'.$this->code.'，请勿泄漏给其他人。';
    }

    public static function send($phone, $type, $scene=null){
        $temp_arr = ['', 'SMS_212481496', 'SMS_212471510'];
        $code = rand(100000, 999999);
        $re = SMS::send($phone, $code, $temp_arr[$type]);
        if($re->Code != 'OK'){
            return Re::data(null, $re->Message, Re::ERROR);
        }
        //$code = '123456';
        $phone_code = new PhoneCode();
        $phone_code->phone = $phone;
        $phone_code->code  = (string)$code;
        if($phone_code->save()){
            return Re::data(['data' => $code]);
        }else{
            return Re::data(null, $phone_code->showErrors(), Re::ERROR);
        }
    }

    // const SCENE_OFFICIAL_SIGN_UP = 'official_sign_up';
    // const SCENE_OFFICIAL_BIND_PHONE = 'official_bind_phone';
    // const SCENE_OFFICIAL_CHANGE_PHONE = 'official_change_phone';
    // const SCENE_OFFICIAL_FORGET_PASSWORD = 'official_forget_password';
    // const SCENE_OFFICIAL_SIGN_IN = 'official_sign_in';
    // const SCENE_ZHAOPIN_FORGET_PASSWORD = 'zhaopin_forget_password';
    // const SCENE_ZHAOPIN_SIGN_IN = 'zhaopin_sign_in';

    public static function check($phone, $code, $scene = null){
        $query = PhoneCode::find()
            ->where(['phone'=>$phone])
            ->andWhere('created_at > :limit_time', [':limit_time'=>time() - 60*5]);
        if($scene !== null){
            $query->andWhere(['scene'=>$scene]);
        }
        $phone_code = $query
            ->orderBy('id desc')
            ->one();
        /** @var $phone_code PhoneCode */
        if($phone_code && $phone_code->code == $code){
            $phone_code->status = PhoneCode::CHECKED;
            $phone_code->save();
            return true;
        }else{
            return false;
        }
    }

    public static function preCheck($phone, $code, $scene = null){
        $query = PhoneCode::find()
            ->where(['phone'=>$phone])
            ->andWhere('created_at > :limit_time', [':limit_time'=>time() - 60*3]);
        if($scene !== null){
            $query->andWhere(['scene'=>$scene]);
        }
        $phone_code = $query
            ->orderBy('id desc')
            ->one();
        /** @var $phone_code PhoneCode */
        if($phone_code && $phone_code->code == $code){
            return true;
        }else{
            return false;
        }
    }

}
